<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

if (isset($_POST["uid"]) && isset($_POST["Password"])&& isset($_POST["newname"])&&isset($_SESSION["username"])&&($_SESSION["username"]!="")) {
    $newname=$_POST["newname"];
    $id=$_POST["uid"];
    $Password=$_POST["Password"];
    $username=$_SESSION["username"];
    $oldpassword=$database->select("user_text",["[>]pass_key"=>["id"=>"uid"]],["salt","password"],["username"=>$username]);
    $salt=$oldpassword[0]["salt"];
    $oldpassword=$oldpassword[0]["password"];
    $Password=md5($Password);
    $Password=hash("sha512",$Password.$salt);
    $Password=hash("sha512",$Password);
    if($Password==$oldpassword){
        $oldname=$database->select("user_group",["id"],["groupname"=>$newname]);
        $oldname=$oldname[0]["id"];
        if($oldname!=null){
            echo "<script>alert('修改失败,新用户组名已存在!');location.href='/admin/lsysg.php';</script>";
        }else{
            $oldgroup=$database->select("user_group",["groupname"],["id"=>$id]);
            $oldgroup=$oldgroup[0]["groupname"];
            $database->update("user_group",["groupname"=>$newname],["id"=>$id]);
            require_once 'getip.php';
            $client_ip = getClientIP();
            $timestamp = time();
            $content = "修改了组名，旧组名是：".$oldgroup."。新组名是：".$newname;
            $database->insert("secure_info", ["user" => $username, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);
            echo "<script>alert('修改成功!');location.href='/admin/lsysg.php';</script>";
        }
    }else{
        echo "<script>alert('密码错误!');location.href='/admin/lsysg.php';</script>";
    }
}


?>